package com.xymtop.Controller;

import com.xymtop.Annotation.Controller;
import com.xymtop.Annotation.Mapping;
import com.xymtop.Annotation.Rest;
import com.xymtop.Entity.Stu;
import com.xymtop.Server.ResoultJson;
import com.xymtop.Server.ServerStatus;
import com.xymtop.Service.StuService;
import com.xymtop.Server.Util.DBService;
import com.xymtop.Vo.StuVo;

import javax.servlet.http.*;
import java.util.List;

@Controller(router = "stu")
public class StudentController extends HttpServlet {

    private final StuService stuService = new StuService();

    @Mapping(url = "login")
    @Rest
    public  ResoultJson<StuVo> login(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        String id = request.getParameter("id");
        String password = request.getParameter("password");

        StuVo stuVo =  stuService.login(id,password);
        if(stuVo==null){
            return  new ResoultJson<>(204);
        }else {
            return new ResoultJson<>(200,stuVo,"");
        }
    }


    @Mapping(url = "regist")
    @Rest
    public  ResoultJson<Stu> regist(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String classid = request.getParameter("class");
        String sex = request.getParameter("sex");

        Stu stu = new Stu();
        stu.setClassid(classid);
        stu.setName(name);
        stu.setPassword(password);
        stu.setSex(sex);

        Stu res = stuService.regist(stu);

        if(res == null){
            return  new ResoultJson<>(ServerStatus.NOTHAVE);
        }else {
            return  new ResoultJson<>(ServerStatus.SUCCESS,res,"注册成功");
        }
    }

    @Mapping(url = "getList")
    @Rest
    public  ResoultJson<Object> getList(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "SELECT * FROM `stu` ";
        if(request.getParameter("class")!=null){
            String classid = request.getParameter("class");
            sql = "SELECT * FROM `stu` where class = "+ classid;
        }

        List<Object> objects = DBService.Get(sql, com.xymtop.Vo.Stu.class.getCanonicalName());
        if(objects.isEmpty()){
            return  new ResoultJson<>(ServerStatus.NOTHAVE);
        }else {
            return  new ResoultJson<>(0,objects,"");
        }
    }

    @Mapping(url = "getStuList")
    @Rest
    public  ResoultJson<Object> getStuList(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        String courseid = request.getParameter("id");
        String sql = "SELECT * FROM `coursegrade` where courseid = "+ courseid;
        List<Object> objects = DBService.Get(sql, com.xymtop.Vo.GradeCouse.class.getCanonicalName());
        if(objects.isEmpty()){
            return  new ResoultJson<>(ServerStatus.NOTHAVE);
        }else {
            return  new ResoultJson<>(0,objects,"");
        }
    }

}
